package br.com.velp.vskygeo.util;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

/**
 *
 * @author Eduardo
 */
public class GoogleMapsUtil {

    private static final String URL_GOOGLE_MAPS = "http://maps.googleapis.com/maps/api/geocode/json?address=";

    public static GoogleGeoCodeResponse.Results[] buscarEnderecos(String enderecoInput) 
            throws JsonSyntaxException, IOException {
        
        Gson gson = new Gson();
        
        GoogleGeoCodeResponse result = gson.fromJson(
                obterCoordenadasEnderecoJson(URLEncoder.encode(enderecoInput, "UTF-8")), 
                GoogleGeoCodeResponse.class);
        
        return result.results;
        
    }

    private static String obterCoordenadasEnderecoJson(String endereco) throws IOException {

        URL url = new URL(URL_GOOGLE_MAPS + endereco + "&sensor=false&oe=utf8");
        URLConnection connection = url.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));

        String inputLine;
        String jsonResult = "";

        while ((inputLine = in.readLine()) != null) {
            jsonResult += inputLine;
        }
        in.close();


        return jsonResult;

    }            
    
    public static String acessarUrl(String urlAcesso) throws IOException {
        
        URL url = new URL(urlAcesso);
        URLConnection connection = url.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));

        String inputLine;
        String result = "";

        while ((inputLine = in.readLine()) != null) {
            result += inputLine;
        }
        
        in.close();


        return result;        
        
    }
      
    public class GoogleGeoCodeResponse {

        public String status;
        public Results[] results;

        public class Results {

            public String formatted_address;
            public Geometry geometry;
            public String[] types;
            public address_component[] address_components;

            public address_component[] getAddress_components() {
                return address_components;
            }

            public void setAddress_components(address_component[] address_components) {
                this.address_components = address_components;
            }

            public String getFormatted_address() {
                return formatted_address;
            }

            public void setFormatted_address(String formatted_address) {
                this.formatted_address = formatted_address;
            }

            public Geometry getGeometry() {
                return geometry;
            }

            public void setGeometry(Geometry geometry) {
                this.geometry = geometry;
            }

            public String[] getTypes() {
                return types;
            }

            public void setTypes(String[] types) {
                this.types = types;
            }
            
        }

        public class Geometry {

            public Bounds bounds;
            public String location_type;
            public Location location;
            public Bounds viewport;

            public Bounds getBounds() {
                return bounds;
            }

            public void setBounds(Bounds bounds) {
                this.bounds = bounds;
            }

            public Location getLocation() {
                return location;
            }

            public void setLocation(Location location) {
                this.location = location;
            }

            public String getLocation_type() {
                return location_type;
            }

            public void setLocation_type(String location_type) {
                this.location_type = location_type;
            }

            public Bounds getViewport() {
                return viewport;
            }

            public void setViewport(Bounds viewport) {
                this.viewport = viewport;
            }
            
        }

        public class Bounds {

            public Location northeast;
            public Location southwest;

            public Location getNortheast() {
                return northeast;
            }

            public void setNortheast(Location northeast) {
                this.northeast = northeast;
            }

            public Location getSouthwest() {
                return southwest;
            }

            public void setSouthwest(Location southwest) {
                this.southwest = southwest;
            }
            
        }

        public class Location {

            public String lat;
            public String lng;

            public String getLat() {
                return lat;
            }

            public void setLat(String lat) {
                this.lat = lat;
            }

            public String getLng() {
                return lng;
            }

            public void setLng(String lng) {
                this.lng = lng;
            }
            
        }

        public class address_component {

            public String long_name;
            public String short_name;
            public String[] types;

            public String getLong_name() {
                return long_name;
            }

            public void setLong_name(String long_name) {
                this.long_name = long_name;
            }

            public String getShort_name() {
                return short_name;
            }

            public void setShort_name(String short_name) {
                this.short_name = short_name;
            }

            public String[] getTypes() {
                return types;
            }

            public void setTypes(String[] types) {
                this.types = types;
            }
             
        }

        public Results[] getResults() {
            return results;
        }

        public void setResults(Results[] results) {
            this.results = results;
        }

        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }
        
    }
}
